import sys

import os
from google.appengine.ext.webapp import template

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.dist import use_library
use_library('django', '0.96')
from os import environ
import models
import datetime

class MainPage(webapp.RequestHandler,db.Model):
	def get(self):
        	user = users.get_current_user()

		if user:
			usercheck=models.userDetails.all()
			usercheck.filter("email =",user.email())
			try:
				currentuser=usercheck.fetch(1)[0]
				userdet=db.GqlQuery("SELECT * FROM userDetails WHERE email='"+user.email()+"'")
				for i in userdet:
					if i.privilege=='admin':
						self.redirect("/admin")
						return
					elif i.approved==False:
						self.redirect("/usererror")
						return
				self.redirect("/user")
			except:
				path = os.path.join(os.path.dirname(__file__),'form.html')#use template
				self.response.out.write(template.render(path,{"email":user.email()}))#using template
		else:
		    self.redirect(users.create_login_url(self.request.uri))

	def post(self):
		user = users.get_current_user()
		details=models.userDetails()
		details.username=self.request.get("name")
		details.email=user.email()
		details.mobilenumber=self.request.get("phone")
		details.privilege="user"
		details.projecteamids=""
		details.admissionnumber=self.request.get("admno")
		details.userid=self.request.get("admno")
		details.branch=self.request.get("branch")
		details.approved=False
		details.put()
		self.redirect("/user")
		
class Display(webapp.RequestHandler):
	def get(self):
        	user = users.get_current_user()

		if user:
			path = os.path.join(os.path.dirname(__file__),'home.html')#use template
			self.response.out.write(template.render(path,{"email":user.email()}))#using template
		else:
		    self.redirect(users.create_login_url(self.request.uri))

class Home(webapp.RequestHandler):
	def get(self):
        	user = users.get_current_user()
		if user:
			usercheck=models.userDetails.all()
			usercheck.filter("email =",user.email())
			try:
				currentuser=usercheck.fetch(1)[0]
				userdet=db.GqlQuery("SELECT * FROM userDetails WHERE email='"+user.email()+"'")
				for i in userdet:
					if i.privilege=='admin':
						self.redirect("/admin")
						return
					elif i.approved==False:
						return
						
				path = os.path.join(os.path.dirname(__file__),'landing.html')#use template
				self.response.out.write(template.render(path,{"email":user.email()}))#using template
			except:
				self.redirect("/")
				
		else:
		    self.redirect(users.create_login_url("/"))

class Logout(webapp.RequestHandler):
	def get(self):
        	user = users.get_current_user()
		if user:
			self.redirect(users.create_logout_url("/"))
		else:
			self.redirect(users.create_login_url("/"))

class Admin(webapp.RequestHandler):
	def get(self):
        	user = users.get_current_user()
		if user:
			usercheck=models.userDetails.all()
			usercheck.filter("email =",user.email())
			try:
				currentuser=usercheck.fetch(1)[0]
				userdet=db.GqlQuery("SELECT * FROM userDetails WHERE email='"+user.email()+"'")
				for i in userdet:
					if i.privilege=='user':
						self.redirect("/")
						return

				path = os.path.join(os.path.dirname(__file__),'admin.html')#use template
				self.response.out.write(template.render(path,{"email":"Administrator"}))#using template
			except:
				self.redirect("/")

		else:
		    self.redirect(users.create_login_url("/"))

application = webapp.WSGIApplication([('/', MainPage),('/inser', Display),('/home', Home),('/logout', Logout),('/admin',Admin)],debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
